package com.hanyxx.thread;

/**
 * suspend()是一个过时方法。
 * 它会使线程进入暂停状态，如果方法有synchronized修饰，那么它会持有锁，造成死锁
 * @author layman
 * @date 2021/2/5
 */
public class ThreadSuspend {
    private static demo03 demo03 = new demo03();

    public static void main(String[] args) {
        ThreadSuspend ts = new ThreadSuspend();
        //设置一个线程A
        Thread t1 = new Thread(() -> {
            while (true) {
                ts.doSomething();
            }
        });
        t1.setName("AAA线程");
        t1.start();
        //设置一个线程B
        Thread t2 = new Thread(() -> {
            while (true) {
                ts.doSomething();
            }
        });
        t2.setName("BBB线程");
        t2.start();
    }
    public void doSomething(){
        demo03.printString();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class demo03{
    private String name = "layman";
    private String password = "12345";

    public synchronized void printString(){
        System.out.println(Thread.currentThread().getName()+"---name:"+name+" ,pasdsword"+password);
        if("AAA线程".equals(Thread.currentThread().getName())){
            Thread.currentThread().suspend();
        }
    }
}